﻿using DoNetDrive.Common.Extensions;
using static DoNetTool.Common.SqlTool;

namespace DoNetTool.Common.Web
{

    /// <summary>
    /// 页面分页查询参数类
    /// </summary>
    public class WebPageBase
    {

        /// <summary>
        /// 请求时发来的参数
        /// </summary>
        public string searchText;
        /// <summary>
        /// 排序字段
        /// </summary>
        public string sortName;

        /// <summary>
        /// 页面数据返回值，包含以查询到的页面结果
        /// </summary>
        public WebPageResult Result;

        //返回的参数

        /// <summary>
        /// 客户端身份ID
        /// </summary>
        public int UserID;
        /// <summary>
        /// 需要返回的列
        /// </summary>
        public string GetFields;

        /// <summary>
        /// 排序类型 asc 升序，desc 降序
        /// </summary>
        public string sortOrder;
        /// <summary>
        /// 设置排序类型, 非 0 值则降序
        /// </summary>
        public int OrderType;

        /// <summary>
        /// 每页的尺寸
        /// </summary>
        public int PageSize;
        /// <summary>
        /// 页码 -- 当前页号
        /// </summary>
        public int pageNumber;

        /// <summary>
        ///  查询条件 (注意: 不要加 where)
        /// </summary>
        public string prWhere;
        /// <summary>
        /// 查询到的总记录数
        /// </summary>
        public int TotalRecords;

        /// <summary>
        /// 初始化基本参数
        /// </summary>
        public WebPageBase()
        {
            Result = new WebPageResult();
            searchText = string.Empty;
            sortName = string.Empty;
            UserID = 0;
            GetFields = "*";
            PageSize = 50;
            pageNumber = 1;
            sortOrder = "asc";
            OrderType = 0;
            prWhere = string.Empty;
            TotalRecords = 0;
        }

        /// <summary>
        /// 根据规则初始化参数类，并检查参数合法性
        /// </summary>
        /// <param name="sName">表或试图的名称</param>
        /// <param name="sFid"></param>
        /// <param name="sSort"></param>
        /// <param name="iUserID"></param>
        public virtual void Init(string sName, string sFid, string sSort, int iUserID)
        {

            UserID = iUserID;

            if (string.IsNullOrEmpty(GetFields))
                GetFields = "*";


            if (GetFields == "*")
                GetFields = sFid;

            
            prWhere = string.Empty;

            searchText = StringUtil.CheckInputText(searchText, 30);
            sortName = StringUtil.CheckInputText(sortName, 30);
            GetFields = StringUtil.CheckInputText(GetFields, 2000);
            sortOrder = StringUtil.CheckInputText(sortOrder, 3);


            if (string.IsNullOrEmpty(sortName))
                sortName = sSort;
            if (!string.IsNullOrEmpty(sortOrder))
                OrderType = sortOrder == "asc" ? 0 : 1;
        }

        /// <summary>
        /// 检查排序字段
        /// </summary>
        /// <param name="sFieldDetails"></param>
        /// <param name="sDefSortName"></param>
        public void CheckSortName(string sFieldDetails, string sDefSortName)
        {
            var oFields = FieldDef.Split(sFieldDetails);
            var f = oFields.Find(x =>
            {
                return x.Name.IsSame(sortName) | x.SQLName.IsSame(sortName);
            });
            if (f == null)
                sortName = sDefSortName;
            else if (sortName != f.SQLName)
                sortName = f.SQLName;
        }
    }

}
